package com.xzkingdee.demo.spider;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hengyi.dzfilter.utils.TextUtils;
import com.xzkingdee.demo.baidu.AuthUtil;
import com.xzkingdee.demo.baidu.ImgCensor;
import org.assertj.core.util.Lists;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class SuperSpider implements PageProcessor {

    private String REGEX_PAGE_URL;

    private List<String> wordList;

    private List<String> imageList;

    private String PATH = "C:/Users/Administrator/Desktop/de-bad";

    private File file;

    public SuperSpider(String url) {
        this.wordList = new ArrayList<>();
        this.imageList = new ArrayList<>();
        this.REGEX_PAGE_URL = url;
        file = new File(PATH);
        if (!file.exists()) {
            if (!file.mkdir()) {
                throw new RuntimeException("文件创建失败");
            }
        }
        this.PATH = PATH + "/" + minusUrl(url);
        file = new File(PATH);
        if (!file.exists()) {
            if (!file.mkdir()) {
                throw new RuntimeException("文件创建失败");
            }
        }
    }

    //配置
    @Override
    public Site getSite() {
        return Site.me();
    }

    @Override
    public void process(Page page) {
        String html = page.getHtml().toString();
        if(TextUtils.existFilter(html)){
            wordList.add(page.getUrl().toString());
        }
        List<String> detailURL = page.getHtml().xpath("//a").links().all();
        for (String str : detailURL) {
            if (str.contains(REGEX_PAGE_URL)) {
                page.addTargetRequest(str);
            }
        }
        List<String> all = page.getHtml().xpath("//img").css("img", "src").all();
        for (String s : all) {
            if (null == s || "".equals(s.trim()))
                continue;
            String[] split = s.split("/");
            try {
                SpiderDownload.download(REGEX_PAGE_URL + s, split[split.length - 1], PATH);
            } catch (IOException e) {
                System.out.println(REGEX_PAGE_URL + s + "==========" + PATH);
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        Spider.create(new SuperSpider("http://192.168.0.152:15281")).addUrl("http://192.168.0.152:15281").thread(1).run();

    }


    public void doAnalysis() {
        System.out.println("====================开始分析==========================");
        String auth = AuthUtil.getAuth();
        List<File> files = Lists.newArrayList(file.listFiles());
        assert files != null;
        files.parallelStream().forEach(one -> {
            String result = ImgCensor.getResult(file.getAbsolutePath(), auth);
            JSONObject jsonObject = JSON.parseObject(result);
//            jsonObject.

        });


    }

    private String minusUrl(String url) {
        url = url.replace("/", "");
        url = url.replace(".", "");
        url = url.replace(":", "");
        if (url.contains("?")) {
            url = url.split("/?")[0];
        }
        return url;
    }


}